package com.financial.business.management.utils;

import java.io.*;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;


/**
 * User: rizenguo
 * Date: 2014/10/23
 * Time: 14:59
 */
public class Util {


    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";    




    public static byte[] readInput(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.close();
        in.close();
        return out.toByteArray();
    }

    public static String inputStreamToString(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString();
    }


    public static InputStream getStringStream(String sInputString) throws UnsupportedEncodingException {
        ByteArrayInputStream tInputStringStream = null;
        if (sInputString != null && !sInputString.trim().equals("")) {
            tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));
        }
        return tInputStringStream;
    }



    public static String getStringFromMap(Map<String, Object> map, String key, String defaultValue) {
        if (key == "" || key == null) {
            return defaultValue;
        }
        String result = (String) map.get(key);
        if (result == null) {
            return defaultValue;
        } else {
            return result;
        }
    }

    public static int getIntFromMap(Map<String, Object> map, String key) {
        if (key == "" || key == null) {
            return 0;
        }
        if (map.get(key) == null) {
            return 0;
        }
        return Integer.parseInt((String) map.get(key));
    }


    /**
     * 读取本地的xml数据，一般用来自测用
     * @param localPath 本地xml文件路径
     * @return 读到的xml字符串
     */
    public static String getLocalXMLString(String localPath) throws IOException {
        return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));
    }

    /**
     * 获取本地ip
     * @return
     */
	public static String  getIP(){
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
			return  addr.getHostAddress();
		} catch (UnknownHostException e) {
            e.printStackTrace();
			return "";
		}
	}
	
	   /**   
	    * 将元为单位的转换为分 替换小数点，支持以逗号区分的金额  
	    * @param yuan
	    * @return  
	    */    
	   public static int changeYuanToFen(double yuan){
	   		//将double元转换成String型 如123.456
			String yuanStr = yuan+"";
			int fen = 0;
			String currency =  yuanStr.replaceAll("\\$|\\￥|\\,", "");  //处理包含, ￥ 或者$的金额  
	        int index = currency.indexOf(".");  
	        int length = currency.length();  
	        Long amLong = 0l;  
	        if(index == -1){  
	            amLong = Long.valueOf(currency+"00");  
	        }else if(length - index >= 3){
	            amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));  
	        }else if(length - index == 2){  
	            amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);  
	        }else{  
	            amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");  
	        }  
	        fen = Integer.parseInt(amLong.toString());
	        return fen;
	   } 
	   /**
	    * 分转元
	    * @param amount
	    * @return
	    * @throws Exception
	    */
	    public static String changeF2Y(String amount) throws Exception{    
	        if(!amount.matches(CURRENCY_FEN_REGEX)) {    
	            throw new Exception("金额格式有误");    
	        }    
	        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();    
	    }    
}



